﻿using IOA.MES.Client.Common;
using IOA.MES.Client.Models.Response;
using IOA.MES.Common;
using NLog;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace IOA.MES.Client.LaminatorRework
{
    //public delegate void SetImageDelegate(string image);
    /// <summary>
    /// WinScan.xaml 的交互逻辑
    /// </summary>
    public partial class WinScan : Window
    {
        private Logger logger = LogManager.GetCurrentClassLogger();
        
        public InspectData InspectData { get; set; }
        private DateTime loadTime;
        public WinScan()
        {
            
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtBarCode1.Text = "";
          
            txtBarCode1.Focus();
            InspectData = new InspectData
            {
                BarCode1 = "",               
                ComponentData = null
            };

            loadTime = DateTime.Now;
            this.Topmost = true;
        }

        private void cbCode_Checked(object sender, RoutedEventArgs e)
        {
            var chexkBox = sender as CheckBox;
            var txtBox = (TextBox)FindName($"txt{chexkBox.Name.Replace("cb", "")}");
            txtBox.IsEnabled = chexkBox.IsChecked == true;
            if (chexkBox.IsChecked != true)
            {
                txtBox.Text = "";
            }
        }

        private void txtCode_KeyUp(object sender, KeyEventArgs e)
        {
            
            
            
            
            
            var txtBox = sender as TextBox;
            if ((DateTime.Now - loadTime).TotalMilliseconds < 200)
            {
                txtBox.Text = "";
            }
            if (e.Key != Key.Enter || string.IsNullOrWhiteSpace(txtBox.Text.Trim()))
            {
                return;
            }
            txtBox.Text = txtBox.Text.Trim().ToUpper();

            switch (txtBox.Name)
            {
                case "txtBarCode1":
                    var getLaminatorReworkDataResponse = BaseEnv.InvokeApi<GetLaminatorReworkResponse>("/api/NQC/GetLaminatorReworktData", new { BarCode = txtBarCode1.Text.ToUpper().Trim() });
                    if (getLaminatorReworkDataResponse.ErrCode != 200)
                    {
                        CommonMessage.Alert(this, getLaminatorReworkDataResponse.ErrMsg);
                        txtBarCode1.SelectAll();
                        return;
                    }
                    InspectData.ComponentData = getLaminatorReworkDataResponse.Data;
                    
                    break;
               
                default:

                    break;
            }

            if (string.IsNullOrWhiteSpace(txtBarCode1.Text))
            {
                txtBarCode1.Text = string.Empty;
                txtBarCode1.Focus();
                return;
            }
          

            TrySubmit();
        }

        private void InitForm()
        {
            txtBarCode1.Text = string.Empty;
            txtBarCode1.Focus();
          
        }

        

        private void TrySubmit()
        {
            InspectData.BarCode1 = txtBarCode1.Text.Trim();

            this.DialogResult = true;
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult != true)
            {
                InspectData = new InspectData
                {
                    BarCode1 = "",
                    ComponentData = null
                };
            }
        }
    }

    public class InspectData
    {
        public string BarCode1 { get; set; }
        public GetLaminatorReworkResponse ComponentData { get; set; }
    }
}
